/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package bpm.graphic
{
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.core.DragSource;
	import mx.core.IDataRenderer;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.events.FlexEvent;
	import mx.graphics.ImageSnapshot;
	import mx.managers.DragManager;
	
	import spark.components.Image;

	/*******************************************
	 **** huangjixin,2012-10-16,上午9:23:28 **
	 **** 请一句话表述该类主要作用  **
	 *******************************************/
	public class PanelPlatte extends Graph implements IDataRenderer
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
	
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private var _data:Object;
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		private var _allowDrag:Boolean = true;
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		public function PanelPlatte()
		{
			super();
			
			removeDragListener(this);
	
		}//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
			if(value is Render){
				addElement(Render(value));
			}
		}

		public function get allowDrag():Boolean
		{
			return _allowDrag;
		}

		public function set allowDrag(value:Boolean):void
		{
			_allowDrag = value;
		}

		/**
		 *	对孩子进行所有侦听鼠标按下函数。 
		 * 
		 */
		private function addEventListeners():void
		{
			if(!allowDrag){
				return;
			}
			var nEle:int=this.numElements;
			var uiComponent:UIComponent;
			for (var i:int=0; i < nEle; i++)
			{
				uiComponent=this.getElementAt(i) as UIComponent;
				if(uiComponent.hasOwnProperty("graph")){
					var graph:Graph = uiComponent["graph"];
					removeDragListener(graph);
				}
				uiComponent.addEventListener(MouseEvent.MOUSE_DOWN, onRenderMouseDown);
			}
		}
		
		private function removeDragListener(graph:Graph):void
		{
			graph.removeDragEventListener();
			for (var i:int=0; i < graph.numElements; i++)
			{
				var uiComponent:UIComponent = this.getElementAt(i) as UIComponent;
				if(uiComponent.hasOwnProperty("graph")){
					var gr:Graph = uiComponent["graph"];
					gr.removeDragEventListener();
					removeDragListener(gr);
				}
			}
		}//removeDragListener结束
		/**
		 *	做有关拖拽的准备 
		 * @param event
		 * 
		 */
		protected function onRenderMouseDown(event:MouseEvent):void
		{
			//拖拽事件源
			var dragSrc:UIComponent = event.currentTarget as UIComponent;
			
			//生成拖拽图片
			var image:Image=new Image();
			image.source=new Bitmap(ImageSnapshot.captureBitmapData(event.currentTarget as UIComponent));
			
			var hDistance:Number=0;
			var vDistance:Number=0;
			var uiComponent:UIComponent=UIComponent(event.currentTarget);
			hDistance=mouseX - uiComponent.x;
			vDistance=mouseY - uiComponent.y;
			
			var dragSource:DragSource=new DragSource();
			var object:Object={};
			object.hDistance=hDistance;
			object.vDistance=vDistance;
			dragSource.addData(object, Graph.DATA_FORMAT);
			DragManager.doDrag(uiComponent, dragSource, event,image);
		}
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		override protected function commitProperties():void
		{
			super.commitProperties();
			addEventListeners();
		}
	}//类结束
}//包结束